package org.pps.gl.client.vue;

import java.util.ArrayList;
import org.pps.gl.client.GetInfoService;
import org.pps.gl.client.GetInfoServiceAsync;
import org.pps.gl.client.data.EtudeInfo;
import org.pps.gl.client.data.ExperienceInfo;
import org.pps.gl.client.data.UserInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Classe représentant la visualisation d'un profil par un autre utilisateur.
 * 
 */
public class RDC_Visualisation extends Composite implements
		ValueChangeHandler<String> {
	VerticalPanel visualisation;
	SimplePanel profil;
	VerticalPanel vueExperience;
	VerticalPanel vueEtudes;
	VerticalPanel vueRelations;
	VerticalPanel vueEcrire;
	private ArrayList<EtudeInfo> etudes;
	private ArrayList<ExperienceInfo> experiences;
	private ArrayList<String> contacts;

	public RDC_Visualisation() {
		visualisation = new VerticalPanel();
		visualisation.setStyleName("profilMarge");
		vueEtudes = new VerticalPanel();
		vueExperience = new VerticalPanel();
		vueRelations = new VerticalPanel();
		vueEcrire = new VerticalPanel();
		profil = new SimplePanel();
		profil.setWidth("550px");
		Label titre = new Label("Visualisation");
		titre.setStylePrimaryName("titre");
		visualisation.add(titre);
		visualisation.add(profil);
		initWidget(visualisation);
		// Pps_gl.getCm().addRefreshUserEventHandler(this);
		etudes = new ArrayList<EtudeInfo>();
		experiences = new ArrayList<ExperienceInfo>();
		contacts = new ArrayList<String>();
		History.addValueChangeHandler(this);
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String[] tab = event.getValue().split("/");
		if (tab.length > 1 && tab[0] != null && tab[0].equals("user")) {
			profil.clear();
			TabPanel visu = new TabPanel();
			visu.add(new PLF_V_Identite_ReadOnly(tab[1]), "Identité");
			visu.setSize("100%", "100%");
			visu.getDeckPanel().setSize("100%", "100%");
			visu.selectTab(0);
			visu.add(vueExperience, "Experience");
			visu.add(vueEtudes, "Etudes");
			System.out.println("Visualiser " + tab[1]);
			visu.add(vueRelations, "Relations");
			visu.add(vueEcrire, "Ecrire");
			profil.add(visu);
			getInfo(tab[1]);
		}

	}

	public void getInfo(final String email) {
		GetInfoServiceAsync gi = GWT.create(GetInfoService.class);

		gi.getUserIdentite(email, new AsyncCallback<UserInfo>() {

			@Override
			public void onSuccess(UserInfo result) {
				etudes = result.getSesEtudes();
				experiences = result.getSesExperiences();
				contacts = result.getContacts();
				setInfo(email);
			}

			@Override
			public void onFailure(Throwable caught) {
				
			}
		});
	}

	/**
	 * Affichage des différents onglets du profil visité
	 */
	public void setInfo(String email) {
		// Afficher les études
		vueEtudes.clear();
		if (null != etudes && !etudes.isEmpty()) {
			System.out.println("Il y a " + etudes.size() + " Etudes");
			for (EtudeInfo et : etudes) {
				vueEtudes.add(new PFL_Etude(et.getPeriode(), et.getNomEcole(),
						et.getSpecialite(), et.getIdEtude(), null));
				System.out.println(et.getPeriode() + " " + et.getNomEcole()
						+ " " + et.getSpecialite());
			}
		} else {
			VerticalPanel vp = new VerticalPanel();
			vp.setStyleName("profilMarge");
			Label lblErr = new Label("Aucune étude enregistrée.");
			vp.add(lblErr);
			vp.setCellHorizontalAlignment(lblErr,
					HasHorizontalAlignment.ALIGN_CENTER);
			vp.setCellVerticalAlignment(lblErr,
					HasVerticalAlignment.ALIGN_MIDDLE);
			vp.setSize("300px", "90px");
			vueEtudes.add(vp);
		}

		// Afficher les experiences
		vueExperience.clear();
		if (0 != experiences.size()) {
			for (ExperienceInfo exp : experiences) {
				vueExperience
						.add(new PFL_Experience(exp.getPeriode(), exp
								.getNomEntreprise(), exp.getDirection(), exp
								.getPoste(), exp.getDescription(), exp
								.getDomaine(), exp.getExperienceId(), null));
			}
		} else {
			VerticalPanel vp = new VerticalPanel();
			vp.setStyleName("profilMarge");
			Label lblErr = new Label(
					"Aucune expérience professionelle enregistrée.");
			vp.add(lblErr);
			vp.setCellHorizontalAlignment(lblErr,
					HasHorizontalAlignment.ALIGN_CENTER);
			vp.setCellVerticalAlignment(lblErr,
					HasVerticalAlignment.ALIGN_MIDDLE);
			vp.setSize("300px", "90px");
			vueExperience.add(vp);
		}

		// Afficher les relations
		vueRelations.clear();
		if (0 != contacts.size()) {
			vueRelations.add(new RSX_Relations(contacts));
			vueRelations.setStyleName("profilMarge");
		} else {
			VerticalPanel vp = new VerticalPanel();
			vp.setStyleName("profilMarge");
			Label lblErr = new Label("Aucune relation.");
			vp.add(lblErr);
			vp.setCellHorizontalAlignment(lblErr,
					HasHorizontalAlignment.ALIGN_CENTER);
			vp.setCellVerticalAlignment(lblErr,
					HasVerticalAlignment.ALIGN_MIDDLE);
			vp.setSize("300px", "90px");
			vueRelations.add(vp);
		}
		
		// Onglet pour écrire
		vueEcrire.clear();
		System.out.println("emailVisite "+email);
		vueEcrire.add(new PFL_V_NMessages(email));
		vueEcrire.setStyleName("profilMarge");
	}

}
